<?php
/**
 * CodeController is for user to activate his account.
 * 
 * Bocheng Chen, 18.09.2013
 * 
 * LBB Solutions Oy 版权所有 @ 2013
 */
class CodeController extends Controller
{
	/**
	 * Displays the signup page with code form
	 */
	public function actionIndex()
	{
		if (!Yii::app()->user->isGuest) {
			$this->redirect(Yii::app()->createUrl('/user'), true);
			Yii::app()->end();
		}
		
		$model = new LoginForm('register');
		// if it is ajax validation request
		if(isset($_POST['ajax']) && $_POST['ajax']==='register-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
		
		// collect user input data
		if (isset($_POST['LoginForm']))
		{
			$model->attributes = $_POST['LoginForm'];
			// validate user input and redirect to the previous page if valid
			if ($model->validate() && $model->register()) {
				$this->redirect(Yii::app()->createUrl('/user'), true);
			}
		}
		
		// display the signup page
		if (Yii::app()->request->isAjaxRequest) {
			$this->renderPartial('index', array('model'=>$model));
		} else {
			$this->render('index', array('model'=>$model));
		}
	}
}